package com.skit.customview.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.skit.customview.R;

/**
 * @author shuike <a href="mailto:shuike007@gmail.com">Contact me.</a>
 * @version 1.0
 * @since 2020/6/19
 */
public class SwitchView extends View {

    private final Paint paint;
    private RectF rectF;
    private final Paint circlePaint;
    private boolean checked = false;
    float cx = 0;

    public SwitchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setPadding(dpToPx(5), dpToPx(10), dpToPx(5), dpToPx(10));
    }

    public SwitchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setPadding(dpToPx(5), dpToPx(10), dpToPx(5), dpToPx(10));
    }


    {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(ContextCompat.getColor(getContext(), R.color.view_grey));
        paint.setStyle(Paint.Style.FILL);
        paint.setShadowLayer(10, 0, 5, Color.parseColor("#808080"));

        circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setColor(Color.WHITE);
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setStrokeWidth(10);
//        setBackgroundColor(Color.GREEN);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        setMeasuredDimension(widthMeasureSpec(widthMeasureSpec), heightMeasureSpec(heightMeasureSpec));
    }


    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
        startAnimation();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                setChecked(!checked);
                break;
            case MotionEvent.ACTION_DOWN:
                paint.setColor(ContextCompat.getColor(getContext(), R.color.view_blue));
//                cx = 0;
                invalidate();
                break;
        }
        return true;
    }


    private int dpToPx(int dip) {
        return (int) (Resources.getSystem().getDisplayMetrics().density * dip + 0.5);
    }

    private int heightMeasureSpec(int heightMeasureSpec) {
        int size = dpToPx(25) + getPaddingTop() + getPaddingBottom();
        int result = size;
        int specMode = MeasureSpec.getMode(heightMeasureSpec);
        int specSize = MeasureSpec.getSize(heightMeasureSpec);
        switch (specMode) {
            case MeasureSpec.AT_MOST:
            case MeasureSpec.UNSPECIFIED:
                result = size;
                break;
            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
        }
        return result;
    }

    private int widthMeasureSpec(int widthMeasureSpec) {
        int size = dpToPx(50);
        int result = size;
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);

        switch (specMode) {
            case MeasureSpec.AT_MOST:
            case MeasureSpec.UNSPECIFIED:
                result = size;
                break;
            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
        }
        return result;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        rectF = new RectF(getPaddingLeft(), getPaddingTop(), w - getPaddingRight(), h - getPaddingBottom());
    }

    public void startAnimation() {
        int value = getMeasuredWidth() / 3;
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(value, value * 2);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                cx = (Float) animation.getAnimatedValue();
                postInvalidate();
            }
        });
        valueAnimator.setDuration(200);
        if (isChecked()) {
            valueAnimator.start();
        } else {
            valueAnimator.reverse();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (isChecked()) {
            paint.setColor(ContextCompat.getColor(getContext(), R.color.view_blue));
        } else {
            paint.setColor(ContextCompat.getColor(getContext(), R.color.view_grey));
        }

        canvas.drawRoundRect(rectF, 50, 50, paint);
        int radius = ((getMeasuredWidth() >> 1) / 3) - (getPaddingLeft() >> 1);
//        int cx = getMeasuredWidth() / 3;
        if (cx == 0) cx = (float) getMeasuredWidth() / 3;

        circlePaint.setStyle(isChecked() ? Paint.Style.FILL_AND_STROKE : Paint.Style.STROKE);
        canvas.drawCircle(cx, (getMeasuredHeight() >> 1), radius, circlePaint);
    }
}
